import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by liuben on 2019/9/23.
 */
@Slf4j
public class SHA256 {

    static Logger logger = LoggerFactory.getLogger(SHA256.class);

    public static String getSHA256(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            logger.error("get sha256 nosuch algorithm error:", e);
        } catch (UnsupportedEncodingException e) {
            logger.error("get sha256 nosuch algorithm error:", e);
        }
        return encodeStr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    /**
     * 获取文件的SHA-256值
     *
     * @param fis
     * @return
     */
    public static String getHash256(InputStream fis) {
        String value = "";
        // 获取hash值
        try {
            byte[] buffer = new byte[1024];
            int numRead;
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            do {
                numRead = fis.read(buffer);
                if (numRead > 0) {
                    messageDigest.update(buffer, 0, numRead);
                }
            } while (numRead != -1);
            value = new String(Base64.encodeBase64(messageDigest.digest()));
        } catch (NoSuchAlgorithmException e) {
            log.error("getHash256 NoSuchAlgorithmException", e);
        } catch (FileNotFoundException e) {
            log.error("getHash256 FileNotFoundException", e);
        } catch (IOException e) {
            log.error("getHash256 input close IOException", e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("getHash256 input close IOException", e);
                }
            }
        }
        return value;
    }

    public static String getHash256(byte[] bytes) {
        String value = "";
        // 获取hash值
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(bytes);
            value = new String(Base64.encodeBase64(messageDigest.digest()));
        } catch (NoSuchAlgorithmException e) {
            log.error("getHash256 NoSuchAlgorithmException", e);
        }
        return value;
    }

    public static void main(String[] orgs) {
        System.out.println(getSHA256("459c4726448c24bf4bfe4f6f8d675aa0"));
    }

}
